//
//  GCMIneHomeViewController.m
//  GoldChampion
//
//  Created by Mac on 2018/4/13.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "GCOtherHomeViewController.h"
#import "UICollectionView+STNoresult.h"
#import "STPhotoUrlImageBrowerController.h"
#import "GCWorkCollectionViewCell.h"
#import "GCOtherHomeView.h"
#import "GCOtherHomeCollectionHeader.h"

@interface GCOtherHomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,STPhotoUrlImageBrowerControllerDelegate>
@property(nonatomic, strong) NSArray                     *dataSouce;
@property(nonatomic, strong) UICollectionView                     *collectionView;
@property(nonatomic, weak) STButton                     *iconButton;
@property(nonatomic, strong) GCOtherHomeView                     *header;
@property(nonatomic, strong) UIRefreshControl                     *refreshControl;
@end

@implementation GCOtherHomeViewController

#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self st_hideNavagetionbarAnimated:NO];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self st_showNavagationbarAnimated:NO];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.header = [[GCOtherHomeView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 2)];
    [self.header setOnSelectedMenuHandle:^(NSInteger index) {
        [weakSelf onSelctedHeaderMenuWithIndex:index];
    }];
    [self.view addSubview:self.header];
    
    UICollectionViewFlowLayout * flow = [UICollectionViewFlowLayout new];
    flow.minimumLineSpacing = 0.9;
    flow.minimumInteritemSpacing = 0.9;
    flow.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    flow.headerReferenceSize = CGSizeMake(UIScreenWidth, otherHomeCollectionHeader);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, [TMUtils navgationBarBootom], UIScreenWidth, [TMUtils tabBarTop] -[TMUtils navgationBarBootom] + 49 ) collectionViewLayout:flow];
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[GCWorkCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
    [self.collectionView registerClass:[GCOtherHomeCollectionHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    self.collectionView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, otherHomeCollectionHeader + 100, UIScreenWidth, 400) title:@"" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    [self sendLocalListRequest];
    self.refreshControl = [UIRefreshControl new];
    [self.refreshControl addTarget:self action:@selector(sendLocalListRequest) forControlEvents:UIControlEventValueChanged];
    self.refreshControl.tintColor = [UIColor whiteColor];
    [self.collectionView addSubview:self.refreshControl];
  //  [self.refreshControl beginRefreshing];
    //    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    //        [self sendLocalListRequest];
    //    }];
    //    [self.collectionView.mj_header beginRefreshing];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    if (ios11 && [self.collectionView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
        [self.collectionView setContentInsetAdjustmentBehavior:@(2)];
    }
}
#pragma --mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSouce.count;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(workPhotoItemWith, 150);
}
- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    GCOtherHomeCollectionHeader * header = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    return header;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GCWorkCollectionViewCell * item =[collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    __weak typeof(self) weakSelf =  self;
    [item.iconButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedIconButton:sender indexPath:indexPath];
    }];
    GCItemModel * model = self.dataSouce[indexPath.row];
    [item.iconButton sd_setBackgroundImageWithURL:[NSURL URLWithString:model.img] forState:UIControlStateNormal];
    return item;
}
#pragma --mark UICollectionViewDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.header scrollViewDidScroll:scrollView];
}
#pragma mark --Action Method
- (void)onSelctedIconButton:(id)sender indexPath:(NSIndexPath*)indexPath{
    self.iconButton = sender;
    NSMutableArray * models = [NSMutableArray new];
    for (GCItemModel * item in self.dataSouce) {
        STUrlPhotoModel * model = [STUrlPhotoModel  new];
        model.originImageUrl = item.img;
        model.thumbImageUrl = item.img;
        [models addObject:model];
    }
    STPhotoUrlImageBrowerController * vc = [[STPhotoUrlImageBrowerController alloc] initWithArray:models curentIndex:indexPath.row];
    vc.shouldHideBottomView = YES;
    vc.STPhotoUrlImageBrowerControllerdelegate = self;
    [self presentViewController:vc animated:NO completion:nil];
}
#pragma mark --STPhotoUrlImageBrowerControllerDelegate
- (UIView*)STPhotoSystemBrowserControllerDidScrollToIndexpath:(NSIndexPath*)indexPath model:(STUrlPhotoModel*)model{
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:NO];
    GCWorkCollectionViewCell * item  =(id)[self.collectionView cellForItemAtIndexPath:indexPath];
    return   item.iconButton;
}

#pragma mark --Action Method
- (void)onSelctedHeaderMenuWithIndex:(NSInteger)index{
    if (index == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    if (index == 1) {
        GCSharePresentViewController *vc = [GCSharePresentViewController new];
        [vc setOnSelctedShareHandle:^(NSString *shareName) {
            
        }];
        [self presentViewController:vc animated:NO completion:nil];
    }

}
#pragma mark --NetWork Method
- (void)sendLocalListRequest{
    NSArray *array = [GCItemModel mj_objectArrayWithFilename:@"1.plist"];
    [TMUtils debugSimulationNetWorkWithHadle:^(NSInteger afterTime) {
        self.dataSouce = [array mutableCopy];
        [self.collectionView reloadData];
        [self.refreshControl endRefreshing];
        [self.collectionView.mj_header endRefreshing];
    }];
    
}
@end

